var orderDAO = require('../models/orderDAO')
var cartDAO = require('../models/cartDAO')
var mysql = require('mysql')
var url = require('url')
var ordercontroller = {
    getOrder: function (req, res) {
        orderDAO.getOrder(function (err, results) {
            if (err) {
                console.log(err)
                res.json({ code: 500, data: results, msg: '数据库出错，查询失败！' })
            } else {
                if (results.length > 0) {
                    res.json({ code: 200, data: results, msg: '查询成功' })
                }
                else {
                    res.json({ code: 200, msg: '没有任何订单' })
                }
            }
        })
    },
    getCart: function (req, res) {
        let cartID = req.query.cartID
        orderDAO.getCart(cartID,function (err, results) {
            if (err) {
                console.log(err)
                res.json({ code: 500, msg: '数据库出错，查询失败！' })
            } else {
                if (results.length > 0) {
                    res.json({ code: 200, data: results, msg: '查询成功' })
                }
                else {
                    res.json({ code: 200, msg: '购物车无物品' })
                }
            }
        })
    },
    delOrder: function (req, res) {
        var orderID = req.query.orderID
        console.log(orderID)
        orderDAO.delOrder(orderID, function (err, results) {
            if (err) {
                console.log(err)
                res.json({ code: 500, msg: '数据库出错，无法取消订单！' })
            } else {
                res.json({ code: 200, data: results, msg: '取消成功' })
            }
        })
    },
    insertOrder: function (req, res) {
        var total = req.body.total
        console.log(total)
        var Payment = req.body.Payment
        orderDAO.insertOrder({total:total,Payment:Payment}, function (err, results) {
            if (err) {
                res.json({ code: 500, msg: '数据库出错，无法添加订单' })
            } else {
                res.json({ code: 200, data: results, msg: '添加成功' })
            }
        })
    },
    updOrder: function (req, res) {
        var orderID = req.query.orderID
        var tel = req.query.tel
        var place = req.query.place
        console.log(orderID)
        console.log(tel)
        console.log(place)
        orderDAO.updOrder({ orderID: orderID, tel: tel, place: place }, function (err, results) {
            if (err) {
                console.log(err)
                res.json({ code: 500, msg: '数据库出错，无法修改订单' })
            }
            else {
                res.json({ code: 200, data: results, msg: '修改成功' })
            }
        })
    },
    endOrder: function (req, res) {
        var orderID = req.query.orderID
        orderDAO.endOrder(orderID, function (err, results) {
            if (err) {
                res.json({ code: 500, msg: '数据库出错，无法查看金额' })
            } else {
                res.json({ code: 200, data: results, msg: '查询成功' })
            }
        })
    },
    yesOrder:function(req,res){
        var Payment = req.query.Payment
        orderDAO.yesOrder(Payment,function(err,results){
            if(err){
                console.log(err)
                res.json({code:500,msg:'数据库出错，无法查询'})
            }else{
                res.json({code:200,data:results,msg:'已完成订单'})
            }
        })
    },
    YesOrder:function(req,res){
        var orderID = req.query.orderID
        orderDAO.YesOrder(orderID,function(err,results){
            if(err){
                console.log(err)
                res.json({code:500,msg:'数据库出错，无法查询'})
            }else{
                res.json({code:200,data:results,msg:'已完成订单'})
            }
        })
    },
    noOrder:function(req,res){
        var Payment = req.query.Payment
        orderDAO.noOrder(Payment,function(err,results){
            if(err){
                console.log(err)
                res.json({code:500,msg:'数据库出错，无法查询'})
            }else{
                res.json({code:200,data:results,msg:'未完成订单'})
            }
        })
    },
    selorderdet:function(req,res){
        orderDAO.selorderdet(function(err,results){
            if(err){
                console.log(err)
                res.json({code:500,msg:'数据库出错，无法查询'})
            }else{
                res.json({code:200,data:results,msg:'查询成功'})
            }
        })
    },
    //获取购物车商品信息
    getTrade:function(req,res){
        let userId = req.user.userId
        let cartID = req.query.cartID
console.log(cartID)
        orderDAO.getTrade([userId,cartID],function(err,results){
            if(err){
                console.log(err)
                res.json({code:500,msg:'数据库出错，无法查询'})
            }else{
                res.json({code:200,data:results,msg:'查询成功'})
            }
        })
    },
    insertorderdet:function(req,res){
        var tradeID = req.body.tradeID
        var tradeNum = req.body.tradeNum
        // var orderID = req.body.orderID
        orderDAO.insertorderdet({tradeNum:tradeNum},function(err,results){
            if(err){
                console.log(err)
                res.json({code:500,msg:'数据库出错，插入失败'})
            }else{
                res.json({code:200,data:results,msg:'插入成功'})
            }
        })
    },
    //下订单
    addOrder:function(req,res){
        let userId = req.params.userId
        let cartID = (req.body.cartID)
        let total = req.params.total
        console.log(cartID)
        // cartID.push(0)
        //添加订单表
        orderDAO.addOrder({userId:userId,total:total},function(err,results){
            if(err){
                res.json({code:500,data:-1,msg:'订单失败'})
            }else if(results.affectedRows == 0){
                res.json({code:200,data:-1,msg:"订单失败"})
            }else{
                //添加订单详情
                console.log(results)
                var ids = {cartID,orderID:results.insertId}
                orderDAO.insertorderdet(ids,function(err,results){
                    if(err){
                        res.json({code:500,data:-1,msg:'订单详情失败'})
                    }else if(results.affectedRows == 0){
                        res.json({code:200,data:-1,msg:"订单详情失败"})
                    }else{
                        //删除购物车
                        cartDAO.delAllCart(cartID,function(err,results){
                            if(err){
                                res.json({code:500,data:-1,msg:'删除购物车失败'})
                            }else if(results.affectedRows == 0){
                                res.json({code:200,data:-1,msg:"删除购物车失败"})
                            }else{
                                res.json({code:200,data:results,msg:"删除购物车成功"})
                            }
                        })
                    }
                })
            }
        })
    }
}
module.exports = ordercontroller